import QtQuick
import QtQuick.Controls
import Tool 1.0
Item {
    property alias kCodeNestTreeView_ListModelAlias: kCodeNestTreeView_ListModel
    property color test_color:"yellow"
    property int spacing_treeview: 10
    ListModel {
        id: kCodeNestTreeView_ListModel

    }
    function mapTypeName(name){
        switch(name){
        case "MainExplanation":{
            return "主要说明"
        }
        case "VersionControl":{
            return "版本控制集"
        }
        case "Version":{
            return "版本"
        }
        case "mount":{
            return "挂载"
        }
        case "MountCollection":{
            return "挂载集合"

        }


        }
    }

    ListView {

        anchors.fill: parent
        id: kCodeNestTreeView_TreeView
        model: kCodeNestTreeView_ListModel
        delegate:Column{
            width:kCodeNestTreeView_TreeView.width
            Rectangle {
                width: parent.width
                height: 30
                color: "transparent"

                Text {
                    //     anchors.leftMargin: parent.parent.anchors.leftMargin+40
                    //     anchors.left: parent.left
                    text: model.name + "--" +mapTypeName(model.type)
                    anchors.verticalCenter: parent.verticalCenter
                }

                // 操作按钮

                KOperationButton{
                    // property ListModel kmodel
                    kmodel:model
                    klistmodel: kCodeNestTreeView_ListModel
                    kmodelindex: index
                }
            }




            ListView{
                implicitHeight: contentItem.childrenRect.height
                implicitWidth: parent.width
                model:mount
                delegate: Column{
                    width:kCodeNestTreeView_TreeView.width
                    Rectangle {
                        width: parent.width
                        height: 30
                        color: "transparent"

                        Text {
                            // leftPadding: parent.parent.leftPdding +20
                            anchors.leftMargin: 15
                            anchors.left: parent.left
                            text: model.name + "--" + mapTypeName(model.type)
                            anchors.verticalCenter: parent.verticalCenter
                        }

                        // 操作按钮
                        KOperationButton{
                            // property ListModel kmodel
                            kmodel:model
                            klistmodel: mount
                            // Component.onCompleted: {
                            //     mount.clear()
                            kmodelindex: index
                            // }

                        }
                    }
                    KChildVersionControlWithVersion {
                        width: parent.width
                        itemVersionControlOfKChildVersionControlWithVersion: VersionControl
                        // Component.onCompleted: {
                        //     console.log("VersionControl:", VersionControl)
                        //     console.log("VersionControl name:", VersionControl["name"])
                        // }
                    }



                }

            }



        }
    }


    Connections {
        target: Tool
        function onJoctoqml(jsonObj){
            handleJsonObject(jsonObj)
            // var json=JSON.parse(jsonObj)
            // console.log(jsonObj["code"])
        }
    }
    function createMountCollection(MountCollection){
        var mountCollectionName=MountCollection.name
        var mountCollectionType=MountCollection.type
        return {
            "name":mountCollectionName,
            "type":mountCollectionType,
            "mount":[]
        }
    }

    function createMount(mount){
        var mountName=mount.name
        var mountType=mount.type
        var mountLocalMounting=mount.LocalMounting
        var mountIP=mount.ip
        var mountPort=mount.port
        // console.log(mountIP)
        return {
            "name":mountName,
            "type":mountType,
            "LocalMounting":mountLocalMounting,
            "ip":mountIP,
            "port":mountPort,
            "MainExplanation":{},
            "VersionControl":[],
            "version":[]
        }
    }
    function createMainExplanation(mainExplanation){
        var mainExplanationName=mainExplanation.name
        var mainExplanationType=mainExplanation.type
        return {
            "name":mainExplanationName,
            "type":mainExplanationType,
            "content":{}
        }


    }

    function createVersionControl(VersionControl){
        var versionControlName=VersionControl.name
        var versionControlType=VersionControl.type
        var versionControlMainVersion=VersionControl.MainVersion
        var versionControlMinorVersion=VersionControl.MinorVersion
        var versionControlRevisionVersion=VersionControl.RevisionVersion
        return {
            "name":versionControlName,
            "type":versionControlType,
            "MainVersion":versionControlMainVersion,
            "MinorVersion":versionControlMinorVersion,
            "RevisionVersion":versionControlRevisionVersion,
            "VersionControl":[],
            "version":[]
        }
    }
    function deepCopy(obj) {
        return JSON.parse(JSON.stringify(obj));
    }

    function createVersion(version){
        var versionName=version.name
        var versionType=version.type
        var versionMainVersion=version.MainVersion
        var versionMinorVersion=version.MinorVersion
        var versionRevisionVersion=version.RevisionVersion
        var versionContent=[];
        // version.content
        // console.log(version.content.count)
        for(var i=0;i<version.content.count;i++){
            versionContent.push(deepCopy(version.content.get(i)))
        }
        // console.log(versionContent.text)
        // console.log(versionContent[0])
        // console.log(versionContent)
        return {
            "name":versionName,
            "type":versionType,
            "MainVersion":versionMainVersion,
            "MinorVersion":versionMinorVersion,
            "RevisionVersion":versionRevisionVersion,
            "content":versionContent
        }
    }





    function listModelToObject(listmodel){

        for(var i=0;i<listmodel.count;i++){
            var obj={}
            var model=listmodel.get(i)
            obj["id"]=model.name

            var mountCollectionCreated=createMountCollection(model)

            for(var k=0;k<model.mount.count;k++){
                var mount=model.mount.get(k)
                var mountCreated=createMount(mount)
                // console.log(mount.name)
                for(var j=0;j<mount.VersionControl.count;j++){

                    var versionControl=mount.VersionControl.get(j)
                    var versionControlCreated=null
                    // console.log(versionControl.type)
                    switch(versionControl.type){
                    case "VersionControl":{
                        versionControlCreated=createVersionControl(versionControl)
                        // console.log(123)
                        break;
                    }
                    case "Version":{
                        // console.log(versionControl.name)
                        versionControlCreated=createVersion(versionControl)

                        break;
                    }
                    case "MainExplanation":{
                        // console.log(versionControl.content.get(0).text)
                        versionControlCreated=createVersion(versionControl)
                        // console.log(versionControlCreated.content[0].text)

                        break;
                    }



                    }

                    // console.log(j)
                    versionControlCreated=handleVersionContorl(versionControl,versionControlCreated)
                    // if(versionControl.type==="MainExplanation"){
                    //     console.log(versionControlCreated.content[0].text)
                    // }

                    // console.log(versionControl.name)
                    // var version=versionControl.version
                    // if(version.count!==0){
                    //     for(var b=0;b<version.count;b++){
                    //         var versionCreated=createVersion(version.get(b))
                    //         versionControlCreated=chainVersion(versionCreated,versionControlCreated)
                    //     }
                    // }
                    mountCreated.VersionControl.push(versionControlCreated)
                }

                // var mountCreatedWithVersionControl=handleVersionControl(mount);//递归处理versionControl
                // mountCreated.VersionControl=mountCreatedWithVersionControl.VersionControl

                mountCollectionCreated["mount"].push(mountCreated)
                // console.log(456)
                // console.log(mountCreated.VersionControl[0].type)
                // if(mountCreated.VersionControl[0].type==="MainExplanation"){
                //     console.log(123)
                //     console.log(mountCollectionCreated["mount"][0].VersionControl[0].content[0].text)
                // }


            }
            obj["MountCollection"]=mountCollectionCreated


            Tool.handleQVariantToFile(obj)
        }

    }
    function handleVersionContorl(parent, parentCreated) {
        var versionControlOfParent = parent.VersionControl;
        if (versionControlOfParent) {
            // console.log(1)
            for (var i = 0; i < versionControlOfParent.count; i++) { // 使用 .length
                var versionControlOfParentNum = versionControlOfParent.get(i); // 或用 get(i) 若为特定对象
                var versionControlCreated = createVersionControl(versionControlOfParentNum);
                var versionOfVersionControlOfParent = versionControlOfParentNum.version;
                if (versionOfVersionControlOfParent) {
                    for (var j = 0; j < versionOfVersionControlOfParent.count; j++) { // 使用 .length
                        var version = versionOfVersionControlOfParent.get(j);
                        var versionCreated = createVersion(version);
                        versionControlCreated = chainVersion(versionCreated, versionControlCreated);
                    }
                }
                versionControlCreated = handleVersionContorl(versionControlOfParentNum, versionControlCreated); // 修正函数名
                parentCreated = chainVersionControltoVersionControl(versionControlCreated, parentCreated); // 修正方法名
            }
        }
        return parentCreated;
    }


    function handleJsonObject(jsonObj){
        // console.log(jsonObj)
        if(jsonObj.mount&&Object.keys(jsonObj.mount).length!==0){//mount处理
            // console.log("1")
        }else{//MountCollection
            var mountCollection=jsonObj["MountCollection"]
            var mountCollectionCreated=addMountCollection(mountCollection.type,mountCollection.name)
            // console.log(mountCollectionCreated["type"])
            var mountArrayOfMountCollection=mountCollection["mount"]
            // console.log(mountCollection.mount.length)
            for(var i=0;i<mountArrayOfMountCollection.length;i++){
                // console.log(mountArrayOfMountCollection[i]);
                var mount=mountArrayOfMountCollection[i];
                // console.log(mount.type)
                mount=handleMount(mount)
                // console.log(mount.type)
                // console.log(mount["name"])
                mountCollectionCreated=chainMounttoMountCollection(mount,mountCollectionCreated)
            }




            //最后处理，瓜mountCollectionCreated

            // console.log(mountCollectionCreated.name)
            kCodeNestTreeView_ListModel.append(mountCollectionCreated)
            // kCodeNestTreeView_ListModel.append(mountCollectionCreated)
            // kCodeNestTreeView_ListModel.append(mountCollectionCreated)
            // kCodeNestTreeView_ListModel.append(mountCollectionCreated)
            // kCodeNestTreeView_ListModel.append(mountCollectionCreated)
            // console.log(mountCollectionCreated.mount[0].name)

            // Tool.qvariantmaptoqjsonobject(model)
            listModelToObject(kCodeNestTreeView_ListModel)

            // listModelToObject(kCodeNestTreeView_ListModel)


            // kCodeNestTreeView_ListModel.append(mountCollectionCreated)
            // console.log(mountCollection);
        }



    }



    function handleMount(mount){
        // console.log(mount.LocalMounting)
        var mountCreated=addMount(mount.type,mount.name,mount.LocalMounting,mount.ip,mount.port)
        // console.log(mountCreated.LocalMounting)
        var mainExplanation =mount.MainExplanation
        if(mainExplanation&&Object.keys(mainExplanation).length!==0){
            var mainExplanationCreated=addMainExplanation(mainExplanation.type,mainExplanation.name)
            //挂载并处理mainExplanationCreated content
            // console.log(123)

            //创建并挂载content
            mountCreated=chainMainExplanation(mainExplanationCreated,mountCreated)
            // console.log(mountCreated["type"])
        }




        if(mountCreated.LocalMounting==="true"){//本地，文件处理
            // console.log(123)
            var mountCreatedWithVersionControl=mount;//递归处理versionControl//handleVersionControl(mount)
            // console.log(mountCreatedWithVersionControl.VersionControl[0].content[0].text)
            mountCreated.VersionControl=mountCreatedWithVersionControl.VersionControl
            return mountCreated
        }else{//远程，文件处理

        }

    }


    function handleVersionControl(parent) {
        var versionControl = parent.VersionControl;
        // console.log(versionControl.length)
        for (var i = 0; i < 0; i++) {
            var versionControlNum = versionControl[i];
            console.log(versionControlNum.type)
            var versionControlCreated=null;
            // console.log(versionControlNum.type)
            switch(versionControlNum.type){
            case "VersionControl":{
                versionControlCreated = addVersionControl(
                            versionControlNum.type,
                            versionControlNum.name,
                            versionControlNum.MainVersion,
                            versionControlNum.MinorVersion,
                            );

                versionControlCreated = handleVersionControl(versionControlCreated);

                break;
            }
            case "Version":{
                versionControlCreated = addVersion(
                            versionControlNum.type,
                            versionControlNum.name,
                            versionControlNum.MainVersion,
                            versionControlNum.MinorVersion,
                            versionControlNum.RevisionVersion
                            );


                //处理content
                break;
            }

            case "MainExplanation":{
                versionControlCreated = addMainExplanation(
                            versionControlNum.type,
                            versionControlNum.name,
                            );
                return;
            }

            }


            // 其他操作，比如将其他东西挂到versionControlCreated
            // versionControlCreated = handleVersion(versionControlNum, versionControlCreated);

            // 递归调用本函数
            if (parent.type === "VersionControl") {
                return chainVersionControltoVersionControl(versionControlCreated, parent);
            } else if (parent.type === "mount") {
                return chainVersionControltoMount(versionControlCreated, parent);
            }
        }
        return parent;
    }

    function handleVersion(versionControl, versionControlCreated) {
        // function addVersion(type, name, MainVersion, MinorVersion, RevisionVersion)
        var versions = versionControl.version; // 假设版本信息存储在versions数组中
        for (var i = 0; i < versions.length; i++) {
            var version = versions[i];
            var versionCreated = addVersion(
                        version.type,
                        version.name,
                        version.MainVersion,
                        version.MinorVersion,
                        version.RevisionVersion
                        );
            // 处理version content

            // 挂载version
            versionControlCreated = chainVersion(versionCreated, versionControlCreated);
        }

        return versionControlCreated;
    }


    function addChildRectFile(type,name,loacation,height,xbegin,xend){
        return {
            "type":type,
            "name":name,
            "location":location,
            "height":height,
            "xbegin":xbegin,
            "xend":xend
        }
    }

    function addChildRectText(type,name,content,
                              height,xbegin,xend,
                              fontSize,fontItalic,fontBold){
        return {
            "type":type,
            "name":name,
            "content":content,
            "height":height,
            "xbegin":xbegin,
            "xend":xend,
            "fontSize":fontSize,
            "fontItalic":fontItalic,
            "fontBold":fontBold
        }
    }

    function addChildRectImage(type,name,loacation,height,xbegin,xend){
        return {
            "type":type,
            "name":name,
            "location":location,
            "height":height,
            "xbegin":xbegin,
            "xend":xend
        }

    }
    function handleContent(content){


    }

    function addMountCollection(type,name){
        return {"type":type,"name":name,"mount":[]};
    }
    function chainMounttoMountCollection(Mount,MountCollection){
        MountCollection.mount.push(Mount);
        return MountCollection;
    }
    function chainMounttoModel(Mount){
        kCodeNestTreeView_ListModel.append(Mount)
    }

    function addMount(type,name,LocalMounting,ip,port){
        return {"type":type,"name":name,"LocalMounting":LocalMounting,"ip":ip,"port":port,"MainExplanation":{},"VersionControl":[]}
    }

    function chainMainExplanation(MainExplanation, Mount) {
        Mount["MainExplanation"] = MainExplanation; // 或者使用 Mount.MainExplanation = MainExplanation;
        // console.log(Mount["type"]); // 确保 Mount 对象有 type 属性
        return Mount;
    }

    function addMainExplanation(type,name){
        return {"type":type,
            "name":name,
            "content":{}
        }
    }
    function chainVersionControltoMount(VersionControl,Mount){
        Mount.VersionControl.push(VersionControl)//append
        return Mount
    }
    function chainVersionControltoVersionControl(VersionControl,VersionControlParent){
        VersionControlParent.VersionControl.push(VersionControl)
        return VersionControlParent
    }


    function addVersionControl(type,name,MainVersion,MinorVersion){
        return{
            "type":type,
            "name":name,
            "MainVersion":MainVersion,
            "MinorVersion":MinorVersion,
            "RevisionVersion":0,
            "VersionControl":[],
            "version":[],
            "MainExplanation":{}
        }
    }
    function chainVersion(Version,VersionControl){
        VersionControl.VersionControl.push(Version)
        return VersionControl
    }


    function addVersion(type,name,MainVersion,MinorVersion,RevisionVersion){
        return{
            "name":name,
            "type":type,
            "MainVersion":MainVersion,
            "MinorVersion":MinorVersion,
            "RevisionVision":RevisionVersion,
            "content":{}
        }
    }




}
